package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.system.domain.Gonggao;
import com.ruoyi.system.mapper.GonggaoMapper;
import com.ruoyi.system.service.IGonggaoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 通知公告Service业务层处理
 * 
 * @author zxl
 * @date 2022-12-13
 */
@Service
public class GonggaoServiceImpl implements IGonggaoService 
{
    @Autowired
    private GonggaoMapper gonggaoMapper;

    /**
     * 查询通知公告
     * 
     * @param gonggaoid 通知公告主键
     * @return 通知公告
     */
    @Override
    public Gonggao selectGonggaoByGonggaoid(String gonggaoid)
    {
        return gonggaoMapper.selectGonggaoByGonggaoid(gonggaoid);
    }

    /**
     * 查询通知公告列表
     * 
     * @param gonggao 通知公告
     * @return 通知公告
     */
    @Override
    public List<Gonggao> selectGonggaoList(Gonggao gonggao)
    {
        return gonggaoMapper.selectGonggaoList(gonggao);
    }

    @Override
    public List<Gonggao> selectGonggaoViewList(String userId) {
        return gonggaoMapper.selectGonggaoViewList(userId);
    }

    /**
     * 新增通知公告
     * 
     * @param gonggao 通知公告
     * @return 结果
     */
    @Override
    @Transactional
    public int insertGonggao(Gonggao gonggao)
    {
        int ok=1;
        String jieshourenids[] = gonggao.getJieshourenid().split(",");
        Map map = new HashMap();
        map.put("gonggaoid",gonggao.getGonggaoid());
        map.put("jieshourenids",jieshourenids);
        int result1 = gonggaoMapper.insertGonggaoJieshouren(map);
        if (result1==0){
            ok=0;
            throw new RuntimeException("添加公告接收人关联表失败");
        }
        int result2 = gonggaoMapper.insertGonggao(gonggao);
        if (result2==0){
            ok=0;
            throw new RuntimeException("添加通知公告失败");
        }
        return ok;
    }

    /**
     * 修改通知公告
     * 
     * @param gonggao 通知公告
     * @return 结果
     */
    @Override
    @Transactional
    public int updateGonggao(Gonggao gonggao)
    {
        int ok=1;
        int result1 = gonggaoMapper.updateGonggao(gonggao);
        if (result1==0){
            ok=0;
            throw new RuntimeException("修改通知公告失败");
        }

        String[] gonggaoid = new String[1];
        gonggaoid[0] = gonggao.getGonggaoid();
        int result2 = gonggaoMapper.deleteGonggaoJieshourenByGonggaoids(gonggaoid);
        if (result2==0){
            ok=0;
            throw new RuntimeException("删除公告接收人关联表失败");
        }

        String jieshourenids[] = gonggao.getJieshourenid().split(",");
        Map map = new HashMap();
        map.put("gonggaoid",gonggao.getGonggaoid());
        map.put("jieshourenids",jieshourenids);
        int result3 = gonggaoMapper.insertGonggaoJieshouren(map);
        if (result3==0){
            ok=0;
            throw new RuntimeException("新增公告接收人关联表失败");
        }
        return ok;
    }

    /**
     * 批量删除通知公告
     * 
     * @param gonggaoids 需要删除的通知公告主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteGonggaoByGonggaoids(String gonggaoids)
    {
        int ok=1;
        int result1 = gonggaoMapper.deleteGonggaoByGonggaoids(Convert.toStrArray(gonggaoids));
        if (result1==0){
            ok=0;
            throw new RuntimeException("删除通知公告失败");
        }
        int result2 = gonggaoMapper.deleteGonggaoJieshourenByGonggaoids(Convert.toStrArray(gonggaoids));
        if (result2==0){
            ok=0;
            throw new RuntimeException("删除公告接收人关联表失败");
        }
        return ok;

    }

    /**
     * 删除通知公告信息
     * 
     * @param gonggaoid 通知公告主键
     * @return 结果
     */
    @Override
    public int deleteGonggaoByGonggaoid(String gonggaoid)
    {
        return gonggaoMapper.deleteGonggaoByGonggaoid(gonggaoid);
    }

    @Override
    public List<Gonggao> findNewestByLeixing(String ppGonggaoleixing){
        return gonggaoMapper.findNewestByLeixing(ppGonggaoleixing);
    }
}
